
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>


// Update these with values suitable for your network.
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);
IPAddress dns(192, 168, 1, 1);
EthernetUDP Udp;

void setup()
{
  Serial.begin(9600);
  
  Ethernet.init(5);
  Ethernet.begin(mac, ip, dns);
  Serial.println(Ethernet.localIP());
  
  pinMode(LED_BUILTIN, OUTPUT);
}

void wakeupPC(byte destMac[])
{
    byte all[102];
    int i,c1,j=0;

    IPAddress remoteIp(255,255,255,255);
    
  Serial.println("Sending WOL Packet...");
  digitalWrite(LED_BUILTIN, LOW);

  for(i = 0; i < 6; i++,j++){
    all[j] = 0xFF;
  }
  for(i = 0; i < 16; i++){
    for( c1 = 0; c1 < 6; c1++,j++)
      all[j] = destMac[c1];
  }

  Udp.begin(8888);
  Udp.beginPacket(remoteIp, 9);
  Udp.write(all, 102);
  Udp.endPacket();

  digitalWrite(LED_BUILTIN, HIGH);
}

void loop()
{
  byte wolMac[6] = { 0xF0, 0x2F, 0x74, 0xDD, 0x98, 0x96 };
  if (Serial.available() >0)
  {
    if (Serial.read() == 'w')
    {
      wakeupPC(wolMac);
    }
  }
}
